package com.example.gss.ServiceImpl;

import com.example.gss.entity.Competition;
import com.example.gss.mapper.CompetitionMapper;
import com.example.gss.Service.CompetitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
 * 学科竞赛服务实现类
 */
@Service
@Transactional
public class CompetitionServiceImpl implements CompetitionService {

    @Autowired
    private CompetitionMapper competitionMapper;

    @Override
    public Competition save(Competition competition) {
        competitionMapper.insert(competition);
        return competition;
    }

    @Override
    public Competition findById(String id) {
        return competitionMapper.selectById(id);
    }

    @Override
    public List<Competition> findAll() {
        return competitionMapper.selectAll();
    }

    @Override
    public List<Competition> findByStudentId(String studentId) {
        return competitionMapper.selectByStudentId(studentId);
    }

    @Override
    public List<Competition> findByDateRange(Date startDate, Date endDate) {
        return competitionMapper.selectByDateRange(startDate, endDate);
    }

    @Override
    public Competition update(String id, Competition competition) {
        Competition existing = competitionMapper.selectById(id);
        if (existing != null) {
            competition.setCompetitionId(id);
            competitionMapper.update(competition);
            return competition;
        }
        return null;
    }

    @Override
    public void delete(String id) {
        competitionMapper.deleteById(id);
    }

    @Override
    public Double calculateTotalScore(String studentId) {
        BigDecimal total = competitionMapper.calculateTotalScore(studentId);
        return total != null ? total.doubleValue() : 0.0;
    }
}